package net.game;

import net.util.BooleanUtil;
import net.util.GraphicsUtil;
import net.util.MathUtil;
import net.util.MouseUtil;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Rectangle;

//This is a button
public class Button extends AbstractGUIComponent
{
	private ActionListener listener;
	
	private String text;
	
	public Button(int x, int y, int w, int h)
	{
		bounds = new Rectangle(x,y,w,h);
	}
	
	public void setText(String s)
	{
		text = s;
	}
	
	public void render()
	{
		mousein = MathUtil.inRegion(Mouse.getX(), Mouse.getY(), bounds, true);
		
		final boolean mousedown = mousein && Mouse.isButtonDown(0);
		
		final boolean mousereleased = mousein && MouseUtil.mouseReleased();
		
		if (mousedown)
		{
			GL11.glPushMatrix();
			GL11.glTranslatef(3, 3, 0);
		}
		
		super.render();
		
		if (BooleanUtil.stringIsValid(text))
		{
			GraphicsUtil.DEFAULT_FONT.drawString(bounds.getX(), bounds.getY(), text);
			GL11.glDisable(GL11.GL_TEXTURE_2D);
		}
		
		if (mousedown)
		{
			GL11.glTranslatef(-3, -3, 0);
			GL11.glPopMatrix();
		}
		
		if (mousereleased && listener != null)
		{
			listener.performAction();
		}
	}

	public void setActionListener(ActionListener a)
	{
		listener = a;
	}
}
